<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="生产计划编号" prop="planNumber">
        <el-input
          v-model="queryParams.planNumber"
          placeholder="请输入生产计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="产品编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="计划状态" prop="planStatus">
        <el-select v-model="queryParams.planStatus" placeholder="请选择计划状态">
          <el-option label="全部" value=""></el-option>
          <el-option label="未开始" value="0"></el-option>
          <el-option label="进行中" value="1"></el-option>
          <el-option label="已完成" value="2"></el-option>
        </el-select>
      </el-form-item>



      <el-form-item label="交付日期" prop="creationTime">
        <el-date-picker clearable
          v-model="queryParams.deliveryDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['prod:prod:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['prod:prod:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['prod:prod:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['prod:prod:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prodList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="planId" />
      <el-table-column label="生产计划编号" align="center" prop="planNumber" />
      <el-table-column label="计划状态" align="center" prop="planStatus" > <template slot-scope="scope">
          <span v-if="scope.row.planStatus === 1">未排程</span>
          <span v-else-if="scope.row.planStatus === 2">已排程</span>
        </template> </el-table-column>



<!--      <el-table-column label="优先级" align="center" prop="orderLevel"> <template slot-scope="scope">-->
<!--        <span v-if="scope.row.orderLevel === 1">普通</span>-->
<!--        <span v-else-if="scope.row.orderLevel === 2">紧急</span>-->
<!--        <span v-else>非常紧急</span>-->
<!--      </template> </el-table-column>-->

      <el-table-column label="优先级" align="center" prop="orderLevel" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderLevel===1">普通</el-tag>
          <el-tag v-if="scope.row.orderLevel===2">紧急</el-tag>
          <el-tag v-if="scope.row.orderLevel===3">非常紧急</el-tag>
        </template>
      </el-table-column>


      <el-table-column label="产品编码" align="center" prop="productCode" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="计量单位" align="center" prop="unit" />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="计划产量" align="center" prop="plannedProduction" />
      <el-table-column label="报工数量" align="center" prop="reportedQuantity" />
      <el-table-column label="销售单号" align="center" prop="salesSell" />
      <el-table-column label="交付日期" align="center" prop="deliveryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工艺" align="center" prop="craftId" >
          <template slot-scope="scope">
            <span v-if="scope.row.craftId === 1">
焊接加工通用工艺</span>
            <span v-else-if="scope.row.craftId === 2">精益制造工艺</span>
            <span v-else-if="scope.row.craftId === 3">包装工艺010</span>

            </template>
      </el-table-column>
      <el-table-column label="车间" align="center" prop="shopId">
        <template slot-scope="scope">

          <el-tag v-if="scope.row.shopId === 1">机械制造车间</el-tag>
          <el-tag v-if="scope.row.shopId === 2">组装车间</el-tag>
          <el-tag v-if="scope.row.shopId === 3">包装车间</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="生产线" align="center" prop="productId">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.productId === 1">机械加工生产线A</el-tag>
          <el-tag v-if="scope.row.productId === 2">机械加工生产线B</el-tag>
          <el-tag v-if="scope.row.productId === 3">机械加工生产线C</el-tag>
          <el-tag v-if="scope.row.productId === 4">机械加工生产线D</el-tag>
          <el-tag v-if="scope.row.productId === 5">机械加工生产线E</el-tag>
           </template>
      </el-table-column>
      <el-table-column label="计划开始日期" align="center" prop="startdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束日期" align="center" prop="denddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.denddate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="
实际开始日期" align="center" prop="artdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.artdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束日期" align="center" prop="enddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enddate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="创建日期" align="center" prop="creationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['prod:prod:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['prod:prod:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-data"
            @click="handleView(scope.row)"
            >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产计划表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="生产计划编号" prop="planNumber">
          <el-input v-model="form.planNumber" placeholder="请输入生产计划编号" />
        </el-form-item>

        <el-form-item label="计划状态 " prop="method">
          <el-select v-model="form.planStatus" placeholder="请选择计划状态">
            <el-option label="全部" value=""></el-option>
            <el-option label="未排程" value="1"></el-option>
            <el-option label="已排程" value="2"></el-option>

          </el-select>
        </el-form-item>

        <el-form-item label="优先级" prop="orderLevel">
          <el-input v-model="form.orderLevel" placeholder="请输入优先级" />
        </el-form-item>
        <el-form-item label="产品编码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编码" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="计量单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入计量单位" />
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="计划产量" prop="plannedProduction">
          <el-input v-model="form.plannedProduction" placeholder="请输入计划产量" />
        </el-form-item>
        <el-form-item label="报工数量" prop="reportedQuantity">
          <el-input v-model="form.reportedQuantity" placeholder="请输入报工数量" />
        </el-form-item>
        <el-form-item label="销售单号" prop="salesSell">
          <el-input v-model="form.salesSell" placeholder="请输入销售单号" />
        </el-form-item>
        <el-form-item label="交付日期" prop="deliveryDate">
          <el-date-picker clearable
            v-model="form.deliveryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择交付日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工艺" prop="craftId">
          <el-input v-model="form.craftId" placeholder="请输入工艺" />
        </el-form-item>
        <el-form-item label="车间                                           " prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入车间                                           " />
        </el-form-item>
        <el-form-item label="生产线" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入生产线" />
        </el-form-item>
        <el-form-item label="计划开始日期" prop="startdate">
          <el-date-picker clearable
            v-model="form.startdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划结束日期" prop="denddate">
          <el-date-picker clearable
            v-model="form.denddate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="
实际开始日期" prop="artdate">
          <el-date-picker clearable
            v-model="form.artdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择
实际开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划结束日期" prop="enddate">
          <el-date-picker clearable
            v-model="form.enddate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建日期" prop="creationTime">
          <el-date-picker clearable
            v-model="form.creationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProd, getProd, delProd, addProd, updateProd } from "@/api/prod/prod";

export default {
  name: "Prod",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产计划表表格数据
      prodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planNumber: null,
        planStatus: null,
        orderLevel: null,
        productCode: null,
        productName: null,
        unit: null,
        specification: null,
        plannedProduction: null,
        reportedQuantity: null,
        salesSell: null,
        deliveryDate: null,
        craftId: null,
        shopId: null,
        productId: null,
        startdate: null,
        denddate: null,
        artdate: null,
        enddate: null,
        creator: null,
        creationTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    handleView(row) {
      location.href = '/prod'
    },

    /** 查询生产计划表列表 */
    getList() {
      this.loading = true;
      listProd(this.queryParams).then(response => {
        this.prodList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        planNumber: null,
        planStatus: null,
        orderLevel: null,
        productCode: null,
        productName: null,
        unit: null,
        specification: null,
        plannedProduction: null,
        reportedQuantity: null,
        salesSell: null,
        deliveryDate: null,
        craftId: null,
        shopId: null,
        productId: null,
        startdate: null,
        denddate: null,
        artdate: null,
        enddate: null,
        creator: null,
        creationTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产计划表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planId = row.planId || this.ids
      getProd(planId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产计划表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planId != null) {
            updateProd(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$modal.confirm('是否确认删除生产计划表编号为"' + planIds + '"的数据项？').then(function() {
        return delProd(planIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('prod/prod/export', {
        ...this.queryParams
      }, `prod_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
